<template>
  <div class="page-container">
    <div class="operators">
      <el-button
        v-permission="['menu:add']"
        type="primary"
        @click="addMenuDialog"
        size="medium"
        >新增</el-button
      >
      <el-button
        v-permission="['menu:search']"
        type="primary"
        size="medium"
        @click="getMenusHandle"
        >查询</el-button
      >
    </div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      :height="tableHeight"
      align="center"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="title" label="菜单名称" width="210">
      </el-table-column>
      <el-table-column align="center" prop="name" label="类型" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1" type="success">菜单</el-tag>
          <el-tag v-else type="danger">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="columnFormatter"
        prop="order"
        label="排序"
        width="80"
      >
      </el-table-column>
      <el-table-column
        align="left"
        :formatter="columnFormatter"
        prop="url"
        label="菜单URL"
      >
      </el-table-column>
      <el-table-column
        align="left"
        :formatter="columnFormatter"
        prop="permission"
        label="授权标识"
      >
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-permission="['menu:delete']"
            @click="deleteMenuHandle(scope.row)"
            type="danger"
            size="small"
            >删除</el-button
          >
          <el-button
            @click="addMenuDialog(scope.row)"
            type="primary"
            size="small"
            v-permission="['menu:update']"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <Edit
      title="菜单"
      :formData="form"
      :dialogVisible="menuVisible"
      v-if="menuVisible"
      @closeDialog="closeDialog"
    />
  </div>
</template>

<script>
import Edit from "./edit.vue";
import { getMenus, deleteMenu } from "@/api/sys/sys";
import { autoTableHeight } from "@/utils/tools";
export default {
  data() {
    return {
      form: {
        id: null,
        title: null,
        type: 1,
        parentIds: [],
        url: null,
        order: null,
        permission: null,
      },
      menuVisible: false,
      tableData: [],
      tableHeight: 600,
    };
  },
  components: {
    Edit,
  },
  created() {
    this.getMenusHandle();

    autoTableHeight(220, (e) => {
      this.tableHeight = e;
    });
  },
  methods: {
    addMenuDialog(menu) {
      if (menu.id) {
        this.form = menu;
      } else {
        this.form = {
          parentIds: [],
        };
      }

      this.menuVisible = true;
    },
    deleteMenuHandle(menu) {
      this.$confirm(
        `此操作将永久删除该${menu.type === 1 ? "菜单" : "按钮"}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteMenu({
            id: menu.id,
          }).then((res) => {
            this.getMenusHandle();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getMenusHandle() {
      getMenus().then((res) => {
        this.tableData = res.data;
      });
    },
    closeDialog(isRefresh) {
      this.menuVisible = false;
      this.form = {};
      isRefresh && this.getMenusHandle();
    },
    columnFormatter(row, column, cellValue) {
      return cellValue || "- - -";
    },
  },
};
</script>

<style scoped>
.operators {
  margin-bottom: 16px;
}
.el-cascader {
  width: 100%;
}
.dialog-btns {
  text-align: center;
}
</style>
